package com.hup.utils.io.batch.function;

import com.hup.utils.commons.TextUtil;
import com.hup.utils.io.batch.BaseBatch;
import com.hup.utils.io.batch.ErrRespException;
import com.hup.utils.io.batch.constant.WinServiceStartType;
import com.hup.utils.io.batch.function.ScQc.QcInfoResp;
import lombok.ToString;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;

/**
 * 查询指定服务的详细信息:[启动类型,]
 *
 * @author hugan
 * @date 2020/1/21
 */
@Log4j2
public class ScQc implements BaseBatch<QcInfoResp> {


    private final String serviceName;

    /**
     * @param serviceName 服务名
     */
    public ScQc(String serviceName) {
        this.serviceName = serviceName;
    }

    @Override
    public String cmd() {
        return "sc qc \"" + serviceName + "\"";
    }

    @Override
    public String desc() {
        return "查询[" + serviceName + "]服务信息";
    }

    /**
     * @return nullable null-服务未安装
     */
    @Override
    public QcInfoResp parse(ArrayList<String> lstResp) {
        log.debug("resp.size={}", lstResp.size());
        if (log.isDebugEnabled()) {
            log.debug("[{}]", TextUtil.getLineWrap(lstResp));
        }

        //服务未安装
        if (lstResp.get(2).contains("服务未安装")) {
            /*
            [SC] OpenService 失败 1060:
            (空行)
            指定的服务未安装。
             */
            return null;
        }

        //成功
        ErrRespException.doAssert(lstResp.get(0).contains("成功"), this, lstResp);

        String serviceName = null;
        String line;
        if ((line = lstResp.get(2)).contains("SERVICE_NAME")) {
            int iSt = line.lastIndexOf(": ") + 2;
            serviceName = line.substring(iSt);
            log.debug("serviceName={}", serviceName);
        }

        WinServiceStartType startType = null;
        if ((line = lstResp.get(4)).contains("START_TYPE")) {
            startType = WinServiceStartType.getByCmdResp(line);
            log.debug("startType={}", startType);
        }

        String displayName = null;
        if ((line = lstResp.get(9)).contains("DISPLAY_NAME")) {
            int iSt = line.lastIndexOf(": ") + 2;
            displayName = line.substring(iSt);
            log.debug("displayName={}", displayName);
        }
        return new QcInfoResp(serviceName, startType, displayName);
    }

    /**
     * [SC] QueryServiceConfig 成功
     * <p>
     * SERVICE_NAME: ZhuDongFangYu
     * TYPE               : 10  WIN32_OWN_PROCESS
     * START_TYPE         : 2   AUTO_START
     * ERROR_CONTROL      : 1   NORMAL
     * BINARY_PATH_NAME   : "D:\000Software\360\360Safe\deepscan\zhudongfangyu.exe"
     * LOAD_ORDER_GROUP   : TDI
     * TAG                : 0
     * DISPLAY_NAME       : 主动防御
     * DEPENDENCIES       : RPCSS
     * SERVICE_START_NAME : LocalSystem
     */
    @ToString
    public static class QcInfoResp {

        public final String serviceName;
        public final WinServiceStartType startType;
        public final String displayName;

        private QcInfoResp(String serviceName, WinServiceStartType startType, String displayName) {
            this.serviceName = serviceName;
            this.startType = startType;
            this.displayName = displayName;
        }

    }

}
